package com.moyuwanjia.service.Impl;

import com.moyuwanjia.Dao.IAccountDao;
import com.moyuwanjia.entity.Account02;
import com.moyuwanjia.service.IAccountService;
import com.moyuwanjia.utils.TransactionManager;

import java.util.List;

/**
 * @author 摸鱼丸加~
 * @create 2022-04-24 11:08
 * 该service类中直接使用每个方法中都去手动控制事务，每次都按照逻辑代码去控制事务流程
 * 不但繁琐，而且重复代码较多，一但修改就会显得很麻烦
 */
public class IAccountServiceImpl_old implements IAccountService {
    private IAccountDao accountDao;
    private TransactionManager transactionManager;

    public void setAccountDao(IAccountDao accountDao){
        this.accountDao = accountDao;
    }
    public void setTransactionManager(TransactionManager transactionManager){
        this.transactionManager = transactionManager;
    }


    public List<Account02> findAll() {
        List<Account02> all = null;
        try {
            //1.开启事务
            transactionManager.beginTransaction();
            //2.执行操作
            all = accountDao.findAll();
            //3.提交事务
            transactionManager.commit();
        } catch (Exception e) {
            //5.如果发生异常就回滚
            transactionManager.rollback();
            throw new RuntimeException(e);
        } finally {
            //6.释放连接
            transactionManager.release();
        }
        //4.返回结果
        return all;
    }

    public Account02 findAccountById(Integer id) {
        Account02 accountById =  null;
        try {
            transactionManager.beginTransaction();
            accountById = accountDao.findAccountById(id);
            transactionManager.commit();
        } catch (Exception e) {
           transactionManager.rollback();
           throw new RuntimeException(e);
        } finally{
            transactionManager.release();
        }
        return accountById;
    }

    public void saveAccount(Account02 account02) {
        try {
            transactionManager.beginTransaction();
            accountDao.saveAccount(account02);
            transactionManager.commit();
        } catch (Exception e) {
            transactionManager.rollback();
            throw new RuntimeException(e);
        } finally{
            transactionManager.release();
        }
    }

    public void updateAccount(Account02 account02) {
        try {
            transactionManager.beginTransaction();
            accountDao.updateAccount(account02);
            transactionManager.commit();
        } catch (Exception e) {
            transactionManager.rollback();
            throw new RuntimeException(e);
        } finally{
            transactionManager.release();
        }
    }

    public void deleteAccountById(Integer id) {
        try {
            transactionManager.beginTransaction();
            accountDao.deleteAccountById(id);
            transactionManager.commit();
        } catch (Exception e) {
            transactionManager.rollback();
            throw new RuntimeException(e);
        } finally{
            transactionManager.release();
        }
    }

    /**
     *
     * @param sourceName 转出账户名称
     * @param targetName 转入账户名称
     * @param money 转账金额
     * 转账操作：
     *         1.根据名称查询转出账户
     *         2.根据名称查询转入账户
     *         3.转出账户减金额
     *         4.转入账户加金额
     *         5.更新转入、转出账户
     */
    @Override
    public void transfer(String sourceName, String targetName, Float money) {
        try {
        transactionManager.beginTransaction();
        Account02 accountByName_source = accountDao.findAccountByName(sourceName);//转出账户
        Account02 accountByName_target = accountDao.findAccountByName(targetName);//转入账户
        accountByName_source.setMoney(accountByName_source.getMoney() - money);
        accountByName_target.setMoney(accountByName_target.getMoney() + money);
        //更新转入转出账户
        accountDao.updateAccount(accountByName_source);
        int a = 10/0;
        accountDao.updateAccount(accountByName_target);
        transactionManager.commit();
        } catch (Exception e) {
            transactionManager.rollback();
            throw new RuntimeException(e);
        } finally{
            transactionManager.release();
        }
    }
}
